package com.example.chirdrenserver.controller;


import com.example.chirdrenserver.Holder.UserHolder;
import com.example.chirdrenserver.Pojo.dto.BasePage;
import com.example.chirdrenserver.Pojo.dto.PostAddDTO;
import com.example.chirdrenserver.Pojo.dto.PostContentAddDTO;
import com.example.chirdrenserver.Pojo.dto.PostQueryDTO;
import com.example.chirdrenserver.Pojo.entity.Post;
import com.example.chirdrenserver.Pojo.entity.PostContentMessage;
import com.example.chirdrenserver.Pojo.entity.PostFileMessage;
import com.example.chirdrenserver.Pojo.entity.PostMessage;
import com.example.chirdrenserver.Service.UserService.PostService;
import com.example.chirdrenserver.Utils.Result;
import com.example.chirdrenserver.confg.RabbitMQConfig;
import org.springframework.amqp.core.MessageDeliveryMode;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Random;
import java.util.UUID;

/**
 * @Author: Yu
 * @CreateTime: 2025-06-30
 * @Description:
 */
@RequestMapping("/post")
@RestController
public class PostController {

    @Autowired
    private PostService postService;

    @Autowired
    RabbitTemplate rabbitTemplate;

    /**
     * 获得分页所有帖子
     *
     * @param basePage
     * @return
     */

    @PostMapping("/list")
    public Result getPost(@RequestBody BasePage basePage) {
        String TaskId = UUID.randomUUID().toString();
        PostMessage postMessage = new PostMessage();
        postMessage.setTaskId(TaskId);
        postMessage.setPage(basePage.getPage());
        postMessage.setLimit(basePage.getLimit());
//        boolean sendMessage = sendMessage(TaskId, postMessage);
        return Result.success( postService.getPost(basePage));
    }

    /**
     * 根据标题搜索帖子
     *
     * @param postQueryDTO
     * @return
     */
    @PostMapping("/tit")
    public Result getPostByPostTitle(@RequestBody PostQueryDTO postQueryDTO) {

        return Result.success( postService.getPostByTitle(postQueryDTO));
    }

    /**
     * 根据id获得帖子获得帖子
     *
     * @return
     */

    @PostMapping("/{id}")
    public Result getPostById(@PathVariable Long id) {
        Post post = postService.getById(id);
        return Result.success(post);
    }

    /**
     * 纯文本上传
     * @param postContentAddDTO
     * @return
     */
    @PostMapping("/add/content")
    public Result addContentPost(@RequestBody PostContentAddDTO postContentAddDTO) {
        String TaskId = UUID.randomUUID().toString();
        PostContentMessage postContentMessage = new PostContentMessage();
        postContentMessage.setUserId(UserHolder.get());
        postContentMessage.setTaskId(TaskId);
        postContentMessage.setPostType(postContentAddDTO.getPostType());
        postContentMessage.setContent(postContentAddDTO.getContent());
        postContentMessage.setTitle(postContentAddDTO.getTitle());
        sendContentAddMessage(TaskId, postContentMessage);
        return Result.success();
    }

    /**
     * 带文件上传
     * @param postAddDTO
     * @return
     */
    @PostMapping("/add/file")
    public Result addFilePost(@RequestBody PostAddDTO postAddDTO) {
        String TaskId = UUID.randomUUID().toString();
        PostFileMessage postFileMessage = new PostFileMessage();
        postFileMessage.setUserId(UserHolder.get());
        postFileMessage.setTaskId(TaskId);
        postFileMessage.setPostType(postAddDTO.getPostType());
        postFileMessage.setContent(postAddDTO.getContent());
        postFileMessage.setTitle(postAddDTO.getTitle());
        postFileMessage.setFileId(postAddDTO.getFileId());
        sendFileAddMessage(TaskId,postFileMessage);
        return Result.success();
    }

    /**
     * 发送文本帖消息
     * @param taskId
     * @param postContentMessage
     * @return
     */
    private boolean sendContentAddMessage(String taskId, PostContentMessage postContentMessage) {
        // 发送到RabbitMQ
        rabbitTemplate.convertAndSend(
                RabbitMQConfig.POST_SEND_EXCHANGE,
                RabbitMQConfig.POST_CONTENT_ADD_ROUTING_KEY,
                postContentMessage,
                m -> {
                    // 设置消息属性
                    m.getMessageProperties().setDeliveryMode(MessageDeliveryMode.PERSISTENT);//消息持久化
                    m.getMessageProperties().setCorrelationId(taskId);
                    m.getMessageProperties().setHeader("taskId", taskId);

                    return m;
                }
        );
        return true;
    }

    /**
     * 发送文件帖消息
     * @param taskId
     * @param postFileMessage
     * @return
     */
    private boolean sendFileAddMessage(String taskId, PostFileMessage postFileMessage) {
        // 发送到RabbitMQ
        rabbitTemplate.convertAndSend(
                RabbitMQConfig.POST_SEND_EXCHANGE,
                RabbitMQConfig.POST_ADD_ROUTING_KEY,
                postFileMessage,
                m -> {
                    // 设置消息属性
                    m.getMessageProperties().setDeliveryMode(MessageDeliveryMode.PERSISTENT);//消息持久化
                    m.getMessageProperties().setCorrelationId(taskId);
                    m.getMessageProperties().setHeader("taskId", taskId);

                    return m;
                }
        );
        return true;
    }

    /**
     * 发送缓存消息
     * @param taskId
     * @param postMessage
     * @return
     */
    private boolean sendMessage(String taskId, PostMessage postMessage) {
        // 发送到RabbitMQ
        rabbitTemplate.convertAndSend(
                RabbitMQConfig.POST_SEND_EXCHANGE,
                RabbitMQConfig.POST_SEND_ROUTING_KEY,
                postMessage,
                m -> {
                    // 设置消息属性
                    m.getMessageProperties().setDeliveryMode(MessageDeliveryMode.PERSISTENT);//消息持久化
                    m.getMessageProperties().setCorrelationId(taskId);
                    m.getMessageProperties().setHeader("taskId", taskId);

                    return m;
                }
        );
        return true;
    }


}
